package br.com.carser.dao;

import java.util.List;

import javax.inject.Named;
import javax.persistence.Query;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import br.com.carser.dao.infra.BaseDAO;
import br.com.carser.model.Profile;

/**
 * <B>Project: carser-web</B><BR>
 * 
 * DAO that represents database operations of the Profile entity.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Oct 3, 2011
 * @version 1.0
 */
@Named
public class ProfileDAO extends BaseDAO<Profile> {
	
	private static final String QUERY_GET_NAME = "Profile.getByName";

	/**
	 * Get profile by name with anywhere search method.
	 * 
	 * @param String
	 *            : name.
	 * 
	 * @return List<Profile>: profile list.
	 * 
	 * @throws Throwable
	 *             : connections issues.
	 */
	@SuppressWarnings("unchecked")
	public List<Profile> getByName(String name) {
		Criteria criteria = this.getHibernateCriteria();
		if(name != null && !name.isEmpty()) {
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		return criteria.list();
	}

	public Profile getUniqueByName(String name) {
		Query query = getEntityManager().createNamedQuery(QUERY_GET_NAME);
		query.setParameter("name", name);
		return (Profile) query.getSingleResult();
	}

}
